package com.gfm.story.controller;

import com.gfm.story.constants.CommonConstants;
import com.gfm.story.entity.base.ResultBean;
import com.gfm.story.entity.user.User;
import com.gfm.story.result.ResultMsg;
import com.gfm.story.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @BelongsProject: story
 * @BelongsPackage: com.gfm.story.controller
 * @Author: Administrator
 * @CreateTime: 2019-06-03 10:02
 * @Description: ${Description}
 */
@Api(tags = "用户接口")
@Controller
@RequestMapping(value = "/admin")
public class UserController {

    @Resource
    private UserService userService;




    @RequestMapping("user")
    public String user(ModelMap modelMap){
        modelMap.put("user", new User());
        return "login";
    }

    @RequestMapping("reg")
    public String reg(ModelMap modelMap){
        modelMap.put("user", new User());
        return "reg";
    }

    /**
     * 注册提交 1.注册验证 2.验证失败重定向到注册页面
     *
     * @param user
     * @param modelMap
     * @return
     */
    @RequestMapping("/register")
    @ApiOperation(value = "注册", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "confirmPasswd", value = "重复密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "int", paramType = "query", required = false),
            @ApiImplicitParam(name = "qqNumber", value = "QQ", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "personalWebsite", value = "个人网站", dataType = "String", paramType = "query", required = false)
    })
    public String accountsRegister(User user, ModelMap modelMap) {
        if (user.getUsername() == null || user == null) {//不存在数据跳转到注册页
            modelMap.put("error", "请输入完整的数据");
            return "reg";
        }
        //用户验证
        ResultMsg resultMsg = UserHelper.validate(user);
        //查询账号是否已存在
        if (!userService.IsNotUserName(user)){//存在
            modelMap.put("error", "邮箱已存在");
            return "reg";
        }else{//不存在，可注册
            if (resultMsg.isSuccess() && userService.addUser(user)) {//参数验证成功与添加用户成功
                modelMap.put("success", "注册成功请登录");
                return "login";    //返回注册成功页面
            } else {//验证不成功进行重定向注册页面
                modelMap.put("error", resultMsg.getErrorMsg());
                return "reg";
            }
        }
    }



    /**
     * 登录
     * @param request
     * @return
     */
    @RequestMapping("/login")
    @ApiOperation(value = "登录", httpMethod = "POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "username", value = "用户名", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "target", value = "目标页面", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "loginValidity", value = "账号保存时间", dataType = "int", paramType = "query", required = true),
    })
    public String signin (HttpServletRequest request,User user,ModelMap modelMap){
        System.out.println(user);
        if (user.getUsername() == null || user.getPassword() == null) {
            modelMap.put("error","请输入完整信息");
            return "login";
        }
        //验证用户
        User user1 =userService.getUser(user.getUsername(),user.getPassword());
        if (user1!=null){
            HttpSession session = request.getSession(true);
            session.setAttribute(CommonConstants.USER_ATTRIBUTE, user1);
            session.setAttribute(CommonConstants.PLAIN_USER_ATTRIBUTE, user1);
            return "redirect:/Story/getAllCategoryStoryList?isLogin="+"true";//重定向到目标页或首页
        }else{
            modelMap.put("error","账号信息不正确");
            return "login";
        }
    }

    /**
     * 登出
     * @return
     */
    @RequestMapping("/logOut")
    @ApiOperation(value = "登出", httpMethod = "POST")
    public String logOut(HttpServletRequest request){
        HttpSession session = request.getSession(true);
        session.invalidate();
        return "redirect:/Story/getAllCategoryStoryList?isLogin=false";
    }


    @RequestMapping("/updatePwd")
    public String updatePwd(String username,String password,String cifmPassword,String email){
        if (username==null || password==null || cifmPassword==null || email==null || password!=cifmPassword){
            return "updatepwd";
        }
        boolean flag = userService.updatePwd(username,password,cifmPassword,email);
        if (flag){
            return "home";
        }
        return "updatepwd";
    }

    @RequestMapping("userHref")
    public String userHref(){
        return "mine";
    }

}
